﻿using lso_2u_Web应用开发.App_Code.Service;
using lso_2u_Web应用开发.Models.Database;
using lso_2u_Web应用开发.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Linq;
using lso_2u_Web应用开发.App_Code.RESTful;
using lso_2u_Web应用开发.Models.FormData;

namespace lso_2u_Web应用开发.Controllers
{
    public class WorkController : Controller
    {
        private readonly DressWorkSystemContext _db;
        private SessionHelper sessionHelper;
        private ServiceClient serviceClient;
        private UserService userService;

        public WorkController(DressWorkSystemContext dressWorkSystemContext, IHttpContextAccessor httpContextAccessor)
        {
            _db = dressWorkSystemContext;
            sessionHelper = new SessionHelper(httpContextAccessor.HttpContext.Session);
            serviceClient = new ServiceClient(dressWorkSystemContext);
            userService = serviceClient.getUserService();
        }

        // 货源业务列表
        [HttpGet]
        public IActionResult SupplierList()
        {
            return View();
        }

        // 工厂业务列表
        [HttpGet]
        public IActionResult FactoryList()
        {
            return View();
        }

        // 创建货源
        public IActionResult CreateSupplier()
        {
            return View();
        }


        // === Api ====


        // 获取 工厂列表
        [HttpGet]
        public IActionResult GetFactoryList()
        {
            var list = _db.Factories.Where(x => x.FkBindUser == sessionHelper.UserId)
                .Select(x => new
                {
                    x.Id,
                    x.ChargeName,
                    x.ChargePhoneNumber,
                    x.Address,
                    x.WorkType
                })
                .ToList();

            return ApiResultHelper.SuccessList(list);
        }

        // 获取 业务列表
        [HttpGet]
        public IActionResult GetSupplierList()
        {
            var list = _db.Suppliers.Where(x => x.FkBindUser == sessionHelper.UserId)
                .Select(x => new
                {
                    x.Id,
                    x.ChargeName,
                    x.ChargePhoneNumber,
                    x.Address
                })
                .ToList();

            return ApiResultHelper.SuccessList(list);
        }


        // 创建 货源
        [HttpPost]
        public IActionResult CreateSupplier(SupplierCreate supplierInfo)
        {
            var supplier = new Supplier();
            supplier.ChargeName = supplierInfo.ChargeName;
            supplier.ChargePhoneNumber = supplierInfo.ChargePhoneNumber;
            supplier.Address = supplierInfo.Address;
            supplier.FkBindUser = sessionHelper.UserId;

            _db.Suppliers.Add(supplier);

            var resultCount = _db.SaveChanges();

            if (resultCount <= 0)
            {
                return ApiErrorResult.INSERT_NOT_CHANGE;
            }

            return ApiResultHelper.Success();
        }

        // 添加 货源 使用邀请码
        [HttpPost]
        public IActionResult BindSupplierInvite(string code)
        {
            var supplier = _db.Suppliers.FirstOrDefault(x => x.BindCode == code);
            if (supplier == null)
            {
                return ApiErrorResult.INVITE_UPDATE_NOT_FIND;
            }
            if (supplier.FkBindUser != null)
            {
                return ApiErrorResult.INVITE_UPDATE_BINDED;
            }

            supplier.FkBindUser = sessionHelper.UserId;

            var resultCount = _db.SaveChanges();
            if (resultCount <= 0)
            {
                return ApiErrorResult.UPDATE_NOT_CHANGE;
            }

            return ApiResultHelper.Success();
        }

        // 添加 工厂 使用邀请码
        [HttpPost]
        public IActionResult BindFactoryInvite(string code)
        {
            var factory = _db.Factories.FirstOrDefault(x => x.BindCode == code);
            if (factory == null)
            {
                return ApiErrorResult.INVITE_UPDATE_NOT_FIND;
            }
            if (factory.FkBindUser != null)
            {
                return ApiErrorResult.INVITE_UPDATE_BINDED;
            }

            factory.FkBindUser = sessionHelper.UserId;

            var resultCount = _db.SaveChanges();
            if (resultCount <= 0)
            {
                return ApiErrorResult.UPDATE_NOT_CHANGE;
            }

            return ApiResultHelper.Success();
        }

        // 删除 货源
        [HttpPost]
        public IActionResult DeleteSupplier(int id)
        {
            var supplier = _db.Suppliers.FirstOrDefault(x => x.Id == id);
            if (supplier == null)
            {
                return ApiErrorResult.DELETE_NOT_FOUND;
            }

            // 进行软删除的情况如下: 设置了绑定码 或 已有订单记录
            var softDeleteMode = false;
            if (supplier.BindCode != null)
            {
                softDeleteMode = true;
            }
            if (_db.Orders.Any(x=> x.FkSupplierId == supplier.Id))
            {
                softDeleteMode = true;
            }

            if (softDeleteMode)
            {
            supplier.FkBindUser = -supplier.FkBindUser;
            }
            else
            {
                _db.Suppliers.Remove(supplier);
            }



            var resultCount = _db.SaveChanges();
            if (resultCount <= 0)
            {
                return ApiErrorResult.DELETE_NOT_DELETE;
            }

            return ApiResultHelper.Success();
        }
    }
}
